﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace OA
{
    public partial class OAMain : Form
    {
        #region 定义字段
        public static CookiesEntity cookies = null;
        public SignInfoEntity signinfo = new SignInfoEntity();
        Tools tools = new Tools();
        public delegate string GetOnlineCountDelegate(string cook);
        public delegate void SetTextDelegate(string text);
        public delegate void SetEntityDelegate(CookiesEntity entity);
        public delegate CookiesEntity GetSignOpeateDelegate(HttpHelper.ESignType type, string cookie);
        public delegate string GetMySignDelegate(string cook);
        public delegate string GetMyTaskDelegate(string cook);
        public delegate string GetMyLeaveDelegate(string cook);
        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        public OAMain()
        {
            InitializeComponent();
        }
        private void OAMain_Load(object sender, EventArgs e)
        {
            //TaskIcon.ShowBalloonTip(5000);
            tsslblInfo.Text = "状态：未运行 ----- 助手运行时间：" + Tools.GetTime();
            lblCurrentTime.Text = "当前时间：" + Tools.GetTime();
            tabControl1.Enabled = false;
        }

        private void OAMain_Shown(object sender, EventArgs e)
        {
            Login login = new Login();
            login.Owner = this;
            login.ShowDialog();
        }
        #region 实现内部、外部接口
        /// <summary>
        /// 设置Session内容
        /// </summary>
        /// <param name="session"></param>
        public void SetSession(string session)
        {
            this.txtSession.Text = session;
        }
        /// <summary>
        /// 运行一次事件模拟
        /// </summary>
        public void RunOne()
        {
            tsslblInfo.Text = "状态：已运行 ----- 开始运行时间：" + Tools.GetTime();
            tabControl1.Enabled = true;
            GenSignTime();
            MTime.Enabled = true;
            HeartTime.Enabled = true;
            lbLog.Items.Clear();
            lbLog.Items.Add("用户：" + cookies.UserName + " 已经登录！执行中 --- " + Tools.GetTime());
            GetOnlinePerson();
            TaskIcon.Text = "OA助手 - 用户：" + cookies.UserName + " 已登录";
        }
        #region 获取在线人数并显示
        /// <summary>
        /// 获取在线人数
        /// </summary>
        private void GetOnlinePerson()
        {
            GetOnlineCountDelegate getonlinecountdelegate = new GetOnlineCountDelegate(HttpHelper.GetOnlineCount);
            getonlinecountdelegate.BeginInvoke(cookies.GetCookie, GetOnlineCountCallback, getonlinecountdelegate);
        }
        public void GetOnlineCountCallback(IAsyncResult async)
        {
            GetOnlineCountDelegate dl = (GetOnlineCountDelegate)async.AsyncState;
            string onlinecount = dl.EndInvoke(async);
            if (lblOnline.InvokeRequired)
            {
                SetTextDelegate settextdelegate = new SetTextDelegate(SetOnlineText);
                lblOnline.Invoke(settextdelegate, onlinecount);
            }
        }
        public void SetOnlineText(string onlinecount)
        {
            lblOnline.Text = "当前在线：" + onlinecount + " 人   最后刷新时间：" + Tools.GetTime();
        }
        #endregion

        /// <summary>
        /// 生成随机时间
        /// </summary>
        private void GenSignTime()
        {
            Random r1 = new Random();
            dtpTime1.Value = DateTime.Parse("08:4" + r1.Next(2, 5) + ":" + r1.Next(0, 5) + r1.Next(0, 9));
            dtpTime2.Value = DateTime.Parse("11:3" + r1.Next(2, 5) + ":" + r1.Next(0, 5) + r1.Next(0, 9));
            dtpTime3.Value = DateTime.Parse("12:0" + r1.Next(2, 5) + ":" + r1.Next(0, 5) + r1.Next(0, 9));
            dtpTime4.Value = DateTime.Parse("17:0" + r1.Next(2, 5) + ":" + r1.Next(0, 5) + r1.Next(0, 9));
        }
        #endregion


        private void HeartTime_Tick(object sender, EventArgs e)
        {
            GetOnlinePerson();
        }

        private void MTime_Tick(object sender, EventArgs e)
        {
            lblCurrentTime.Text = "当前时间：" + Tools.GetTime();

            #region 时间禁用设置
            DateTime sourceDate1 = Convert.ToDateTime("09:05:00");
            DateTime currentDate1 = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss"));
            if (currentDate1 > sourceDate1)
            {
                dtpTime1.Enabled = false;
                btnSign1.Enabled = false;
            }
            DateTime sourceDate2 = Convert.ToDateTime("12:30:00");
            DateTime currentDate2 = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss"));
            if (currentDate2 > sourceDate2)
            {
                dtpTime2.Enabled = false;
                btnSign2.Enabled = false;
            }
            DateTime sourceDate3 = Convert.ToDateTime("12:30:00");
            DateTime currentDate3 = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss"));
            if (currentDate3 > sourceDate3)
            {
                dtpTime3.Enabled = false;
                btnSign3.Enabled = false;
            }
            DateTime sourceDate4 = Convert.ToDateTime("23:59:59");
            DateTime currentDate4 = Convert.ToDateTime(DateTime.Now.ToString("HH:mm:ss"));
            if (currentDate4 > sourceDate4)
            {
                dtpTime4.Enabled = false;
                btnSign4.Enabled = false;
            }

            #endregion

            if (dtpTime1.Value.ToString("HH:mm:ss") == Tools.GetTime("HH:mm:ss"))
            {
                if (cbTime1.Checked)
                {
                    GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                    getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign1, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);
                }
            }
            else if (dtpTime2.Value.ToString("HH:mm:ss") == Tools.GetTime("HH:mm:ss"))
            {
                if (cbTime2.Checked)
                {
                    GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                    getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign2, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);
                }
            }
            else if (dtpTime3.Value.ToString("HH:mm:ss") == Tools.GetTime("HH:mm:ss"))
            {
                if (cbTime3.Checked)
                {
                    GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                    getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign3, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);
                }
            }
            else if (dtpTime4.Value.ToString("HH:mm:ss") == Tools.GetTime("HH:mm:ss"))
            {
                if (cbTime4.Checked)
                {
                    GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                    getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign4, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);
                }
            }
        }

        private void btnSign1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认手动操作？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign1, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);
            }
        }

        private void btnSign2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认手动操作？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign2, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);

            }
        }

        private void btnSign3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认手动操作？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign3, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);

            }
        }

        private void btnSign4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认手动操作？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                GetSignOpeateDelegate getsignopeatedelegate = new GetSignOpeateDelegate(HttpHelper.GetSignOpeate);
                getsignopeatedelegate.BeginInvoke(HttpHelper.ESignType.Sign4, cookies.GetCookie, GetSignOpeateCallback, getsignopeatedelegate);

            }
        }
        #region GetSignOpeateDelegate 相关方法
        /// <summary>
        /// 签到的代理方法
        /// </summary>
        /// <param name="async"></param>
        public void GetSignOpeateCallback(IAsyncResult async)
        {
            GetSignOpeateDelegate dl = async.AsyncState as GetSignOpeateDelegate;
            CookiesEntity entity = dl.EndInvoke(async);
            if (lblSignStatus.InvokeRequired)
            {
                SetEntityDelegate settextdelegate = new SetEntityDelegate(SetSignUpdate);
                lblSignStatus.Invoke(settextdelegate, entity);
            }
        }
        private void SetSignUpdate(CookiesEntity entity)
        {
            //lblSignStatus.Text = entity.ResultSignMessage;
            lblSignStatus.Text = "状态：『" + entity.ResultSignMessage + "』打卡成功，时间：" + Tools.GetTime();
            lbLog.Items.Add("状态：『" + entity.ResultSignMessage + "』打卡成功，时间：" + Tools.GetTime());
            txtSystemLog.Text = entity.ResultMessage;
            TaskIcon.ShowBalloonTip(10000, "状态", "状态：『" + entity.ResultSignMessage + "』打卡成功，时间：" + Tools.GetTime(), ToolTipIcon.Info);
        }
        #endregion

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            switch (e.TabPageIndex)
            {
                case 0://基本信息

                    break;
                case 1://我的任务
                    GetMyTaskDelegate getmytaskdelegate = new GetMyTaskDelegate(HttpHelper.GetMyTask);
                    getmytaskdelegate.BeginInvoke(cookies.GetCookie, GetMyTaskCallback, getmytaskdelegate);
                    break;
                case 2://我的倒休
                    GetMyLeaveDelegate getmyleavedelegate = new GetMyLeaveDelegate(HttpHelper.GetMyLeave);
                    getmyleavedelegate.BeginInvoke(cookies.GetCookie, GetMyLeaveCallback, getmyleavedelegate);
                    break;
                case 3://我的签到
                    GetMySignDelegate getmysigndelegate = new GetMySignDelegate(HttpHelper.GetMySign);
                    getmysigndelegate.BeginInvoke(cookies.GetCookie, GetMySignCallback, getmysigndelegate);
                    break;
                case 4://系统日志

                    break;
            }
        }
        private void GetMySignCallback(IAsyncResult async)
        {
            GetMySignDelegate dl = async.AsyncState as GetMySignDelegate;
            string resultmsg = dl.EndInvoke(async);
            signinfo.HTML = resultmsg.Substring(1313);
            //signinfo.MySign1 = resultmsg.Substring(1900, 8);
            if (wbSignIE.InvokeRequired)
            {
                SetTextDelegate settextdelegate = new SetTextDelegate(WebSignCallback);
                wbSignIE.Invoke(settextdelegate, signinfo.HTML);
            }

        }
        private void WebSignCallback(string html)
        {
            wbSignIE.DocumentText = html;
        }
        private void GetMyTaskCallback(IAsyncResult async)
        {
            GetMyTaskDelegate dl = async.AsyncState as GetMyTaskDelegate;
            string resultmsg = dl.EndInvoke(async);
            if (wbMyTask.InvokeRequired)
            {
                SetTextDelegate settextdelegate = new SetTextDelegate(WebTaskCallback);
                wbSignIE.Invoke(settextdelegate, resultmsg);
            }

        }
        private void WebTaskCallback(string html)
        {
            wbMyTask.DocumentText = html;
        }

        private void GetMyLeaveCallback(IAsyncResult async)
        {
            GetMyLeaveDelegate dl = async.AsyncState as GetMyLeaveDelegate;
            string resultmsg = dl.EndInvoke(async);
            int a = resultmsg.IndexOf("<span class=\"big3\">");
            int b = resultmsg.Substring(a).IndexOf("</span><br>")+7;
            string s = resultmsg.Substring(a,b);
            if (wbIELeave.InvokeRequired)
            {
                SetTextDelegate settextdelegate = new SetTextDelegate(WebLeaveCallback);
                wbIELeave.Invoke(settextdelegate, s);
            }

        }
        private void WebLeaveCallback(string html)
        {
            wbIELeave.DocumentText = html;
        }
        private void btnGOIE_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "iexplore.exe";
            process.StartInfo.Arguments = "http://oa.hdsxtech.com:8080/logincheck.php?UNAME=" + cookies.UserName + "&PASSWORD=" + cookies.Password;
            process.Start();
            //OpenIEForm ie = new OpenIEForm(cookies.UserName,cookies.Password);
            //ie.Show();

        }

        private void OAMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tabControl1.Enabled)
            {
                e.Cancel = true;
                this.Hide();
                TaskIcon.ShowBalloonTip(3000, "双击托盘恢复", "我没有关闭喔，只是藏起来了，嘘！", ToolTipIcon.Info);
            }
            else
            {
                Application.Exit();
            }

        }

        private void TaskIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void 关于我AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "iexplore.exe";
            process.StartInfo.Arguments = "http://www.imyy.org/";
            process.Start();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("确认退出助手？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes))
            {
                Application.ExitThread();
                Application.Exit();

            }
        }

    }
}
